{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying Workflow (Lattice) Execution Status in a Notebook\n",
    "\n",
    "Once you have dispatched a workflow, use the Covalent `Result` object to check run status of the lattice programmatically. You can also check the status of [individual electrons](./query_electron_execution_status.ipynb).\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Define and [run a workflow](../execution/execute_lattice.ipynb):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "import covalent as ct\n",
    "\n",
    "@ct.electron\n",
    "def add(x, y):\n",
    "    return x + y\n",
    "\n",
    "@ct.electron\n",
    "def multiply(x, y):\n",
    "    time.sleep(5)\n",
    "    return x * y\n",
    "\n",
    "@ct.lattice\n",
    "def workflow(x, y):\n",
    "    res_1 = add(x=x, y=y)\n",
    "    return multiply(x=res_1, y=y)\n",
    "\n",
    "dispatch_id = ct.dispatch(workflow)(x=2, y=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. Retrieve the `result` object using the dispatch ID with the `ct.get_result()` function. \n",
    "\n",
    "To retrieve the result only once the computation is completed, set `wait=True`. Otherwise, set `wait=False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = ct.get_result(dispatch_id=dispatch_id, wait=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. The dispatch status is maintained in the `status` attribute of the result object. While the computations are running, the status is `RUNNING`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RUNNING\n"
     ]
    }
   ],
   "source": [
    "print(result.status)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. The result object is updated continuously until the workflow is finished. The final status can be one of several values, including `COMPLETED`, `FAILED`, and `CANCELED`.\n",
    "\n",
    "To check the computation status again, re-retrieve the results object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "COMPLETED\n"
     ]
    }
   ],
   "source": [
    "result = ct.get_result(dispatch_id=dispatch_id, wait=True)\n",
    "print(result.status)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Querying Task (Electron) Status in a Notebook](./query_electron_execution_status.ipynb)\n",
    "\n",
    "[Running a Workflow](../execution/execute_lattice.ipynb)\n",
    "\n",
    "[Querying Lattice Execution Time](./query_lattice_execution_time.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
